<?php
define( 'TEMPPATH', get_bloginfo( 'stylesheet_directory' ) );
define( 'IMAGES', TEMPPATH . '/images' );

function get_vid_from_post( $post_id = NULL ) {
	// Get the post ID if none is provided
	if ( $post_id == null OR $post_id == '' ) $post_id = get_the_ID();

	// Get the post or custom field to search
	if ( $video_key = get_option( 'video_thumbnails_custom_field' ) ) {
		$markup = get_post_meta( $post_id, $video_key, true );
	} else {
		$post_array = get_post( $post_id );
		$markup = $post_array->post_content;
		$markup = apply_filters( 'the_content', $markup );
	}

	// Simple Video Embedder Compatibility
	if ( function_exists( 'p75HasVideo' ) ) {
		if ( p75HasVideo( $post_id ) ) {
			$markup = p75GetVideo( $post_id );
		}
	}

	// Checks for the old standard YouTube embed
	preg_match( '#<object[^>]+>.+?https?://www\.youtube(?:\-nocookie)?\.com/[ve]/([A-Za-z0-9\-_]+).+?</object>#s', $markup, $matches );

	// More comprehensive search for YouTube embed, redundant but necessary until more testing is completed
	if ( !isset( $matches[1] ) ) {
		preg_match( '#https?://www\.youtube(?:\-nocookie)?\.com/[ve]/([A-Za-z0-9\-_]+)#', $markup, $matches );
	}

	// Checks for YouTube iframe, the new standard since at least 2011
	if ( !isset( $matches[1] ) ) {
		preg_match( '#https?://www\.youtube(?:\-nocookie)?\.com/embed/([A-Za-z0-9\-_]+)#', $markup, $matches );
	}

	// Checks for any YouTube URL. After http(s) support a or v for Youtube Lyte and v or vh for Smart Youtube plugin
	if ( !isset( $matches[1] ) ) {
		preg_match( '#(?:https?(?:a|vh?)?://)?(?:www\.)?youtube(?:\-nocookie)?\.com/watch\?.*v=([A-Za-z0-9\-_]+)#', $markup, $matches );
	}

	// Checks for any shortened youtu.be URL. After http(s) a or v for Youtube Lyte and v or vh for Smart Youtube plugin
	if ( !isset( $matches[1] ) ) {
		preg_match( '#(?:https?(?:a|vh?)?://)?youtu\.be/([A-Za-z0-9\-_]+)#', $markup, $matches );
	}

	// Checks for YouTube Lyte
	if ( !isset( $matches[1] ) && function_exists( 'lyte_parse' ) ) {
		preg_match( '#<div class="lyte" id="([A-Za-z0-9\-_]+)"#', $markup, $matches );
	}

	// If we've found a YouTube video ID, create the thumbnail URL
	if ( isset( $matches[1] ) ) {
		return $matches[1];
	}

	return NULL;
}

function get_youtube_url_from_post( $post_id = NULL ) {
	return 'http://www.youtube.com/watch?v=' . get_vid_from_post( $post_id );
}

function youtube_url_from_post( $post_id = NULL ) {
	print get_youtube_url_from_post( $post_id );
}

/**
 * Overwrite twentyeleven_setup in twentyeleven theme
 */
function twentyeleven_setup() {

  // This theme styles the visual editor with editor-style.css to match the theme style.
  add_editor_style();

  // Grab Twenty Eleven's Ephemera widget.
  require( get_template_directory() . '/inc/widgets.php' );

  // Add default posts and comments RSS feed links to <head>.
  add_theme_support( 'automatic-feed-links' );

  // This theme uses wp_nav_menu() in one location.
  register_nav_menu( 'primary', __( 'Primary Menu', 'twentyeleven' ) );

  // Add support for a variety of post formats
  // add_theme_support( 'post-formats', array( 'aside', 'link', 'gallery', 'status', 'quote', 'image' ) );
  remove_theme_support('post-formats');
}

function vsa_pagination() {
  global $wp_query;
  $total_pages = $wp_query->max_num_pages;
  if ($total_pages > 1) {
    $current_page = max(1, get_query_var('paged'));
    echo '<div class="page_nav">';
    echo paginate_links(array(
      'base' => get_pagenum_link(1) . '%_%',
      'format' => '/page/%#%',
      'current' => $current_page,
      'total' => $total_pages,
    ));
    echo '</div>';
  }
}
